package com.googlecode.kipler.satisfiability.clash;

import java.util.Iterator;
import java.util.Set;

import com.googlecode.kipler.container.dl.ABox;
import com.googlecode.kipler.container.dl.DependencyManager;
import com.googlecode.kipler.container.dl.DependencySet;
import com.googlecode.kipler.container.dl.Individual;
import com.googlecode.kipler.container.dl.SHOIQABox;
import com.googlecode.kipler.syntax.concept.Nominal;
import com.googlecode.kipler.syntax.formula.ConceptAssertion;
import com.googlecode.kipler.syntax.formula.Formula;
import com.googlecode.kipler.syntax.formula.SameIndividualAssertion;

/**
 * 
 * @author İnanç Seylan
 * 
 */
public class NominalClashDetectionStrategy implements ClashDetectionStrategy {
	private DependencySet dependencySet = new DependencySet();

	private String explanation;

	public String getExplanation() {
		return explanation;
	}

	@Override
	public Set<Formula> getClashCause() {
		// TODO Auto-generated method stub
		return null;
	}

	public DependencySet getDependencySet() {
		return dependencySet;
	}

	public boolean hasClash(ABox abox, Formula formula) {
		boolean hasClash = false;
		if (formula instanceof ConceptAssertion) {
			ConceptAssertion ca = (ConceptAssertion) formula;
			if (ca.getConcept() instanceof Nominal) {
				Nominal nominal = (Nominal) ca.getConcept();
				if (!nominal.isNegated()) {
					SHOIQABox shoiqABox = (SHOIQABox) abox;
					Iterator<Individual> i = shoiqABox.getNominals().iterator();
					while (i.hasNext() && !hasClash) {
						Individual indv = i.next();
						SameIndividualAssertion ia = new SameIndividualAssertion(
								ca.getName(), indv.getName()).toggleNegated();
						if (indv.contains(nominal) && abox.contains(ia)) {
							hasClash = true;
							dependencySet.clear();
							DependencyManager depMan = abox.getWorld().getWcs()
									.getData().getDependencyManager();
							dependencySet.addAll(depMan.getDependencies(ca));
							dependencySet.addAll(depMan.getDependencies(ia));
							ConceptAssertion otherCa = new ConceptAssertion(
									nominal, indv.getName());
							dependencySet.addAll(depMan
									.getDependencies(otherCa));
							explanation = ca + ", " + otherCa + ", and " + ia;
						}
					}
				}
			}
		}
		return hasClash;
	}
}
